module(
    name = "rules_haskell",
    version = "0.17",
)

bazel_dep(
    name = "platforms",
    version = "0.0.6",
)
bazel_dep(
    name = "rules_java",
    version = "6.1.0",
)
bazel_dep(
    name = "rules_license",
    version = "0.0.7",
)
bazel_dep(
    name = "protobuf",
    version = "3.19.0",
    repo_name = "com_google_protobuf",
)
bazel_dep(
    name = "rules_cc",
    version = "0.0.8",
)
bazel_dep(
    name = "rules_nodejs",
    version = "5.8.2",
)
bazel_dep(
    name = "rules_python",
    version = "0.21.0",
)
bazel_dep(
    name = "bazel_skylib",
    version = "1.4.2",
)
bazel_dep(
    name = "rules_proto",
    version = "5.3.0-21.7",
)
bazel_dep(
    name = "aspect_rules_js",
    version = "1.31.0",
)
bazel_dep(
    name = "rules_sh",
    version = "0.3.0",
)

ghc_version = use_extension("//extensions:ghc_version.bzl", "ghc_default_version")
use_repo(
    ghc_version,
    "rules_haskell_ghc_version",
)

rules_haskell_dependencies = use_extension(
    "//extensions:rules_haskell_dependencies.bzl",
    "rules_haskell_dependencies",
)
use_repo(
    rules_haskell_dependencies,
    "Cabal",
    "os_info",
    "rules_haskell_stack",
    "rules_haskell_stack_update",
    "rules_haskell_worker_dependencies",
)

asterius = use_extension("@rules_haskell//haskell/asterius:extension.bzl", "rules_haskell_asterius")
use_repo(
    asterius,
    "rules_haskell_asterius_build_setting",
    "rules_haskell_asterius_webpack",
)

haskell_toolchains = use_extension(
    "@rules_haskell//extensions:haskell_toolchains.bzl",
    "haskell_toolchains",
)
haskell_toolchains.bindists()
use_repo(
    haskell_toolchains,
    "all_bindist_toolchains",
    "rules_haskell_ghc_windows_amd64_cc_toolchain",
    "rules_haskell_python_local",
)

# We need to register the nix toolchain before the bindist ones so it
# take precedence when we use the nixpkgs config.
register_toolchains(
    "@rules_haskell_ghc_nixpkgs_toolchain//:toolchain",
    dev_dependency = True,
)

register_toolchains(
    "@all_bindist_toolchains//:all",
    "@rules_haskell_ghc_windows_amd64_cc_toolchain//:all",
    "@rules_haskell_python_local//:toolchain",
)

# Setup node toolchain and install webpack for asterius.
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
node.toolchain(node_version = "16.9.0")

npm = use_extension(
    "@aspect_rules_js//npm:extensions.bzl",
    "npm",
)
npm.npm_translate_lock(
    name = "rules_haskell_npm",
    link_workspace = "rules_haskell",
    pnpm_lock = "//haskell/asterius/npm:pnpm-lock.yaml",
    verify_node_modules_ignored = "@rules_haskell//:.bazelignore",
)
use_repo(npm, "rules_haskell_npm")

#### Development dependencies ####
bazel_dep(
    name = "rules_nixpkgs_core",
    version = "0.9.0",
    dev_dependency = True,
)
bazel_dep(
    name = "rules_nixpkgs_cc",
    version = "0.9.0",
    dev_dependency = True,
)
bazel_dep(
    name = "rules_nixpkgs_python",
    version = "0.9.0",
    dev_dependency = True,
)
bazel_dep(
    name = "rules_nixpkgs_go",
    version = "0.9.0",
    dev_dependency = True,
)
bazel_dep(
    name = "rules_nixpkgs_posix",
    version = "0.9.0",
    dev_dependency = True,
)
bazel_dep(
    name = "stardoc",
    version = "0.5.4",
    dev_dependency = True,
    repo_name = "io_bazel_stardoc",
)
bazel_dep(
    name = "rules_nixpkgs_nodejs",
    version = "0.9.0",
    dev_dependency = True,
)

non_module_dev_deps = use_extension(
    "//:non_module_dev_deps.bzl",
    "non_module_dev_deps",
    dev_dependency = True,
)
use_repo(
    non_module_dev_deps,
    "examples",
    "examples-arm",
    "tutorial",
)
use_repo(
    non_module_dev_deps,
    "nixpkgs_default",
    "rules_haskell_ghc_nixpkgs",
    "rules_haskell_ghc_nixpkgs_toolchain",
)
use_repo(
    non_module_dev_deps,
    "nixpkgs_config_cc",
    "nixpkgs_config_cc_info",
    "nixpkgs_config_cc_toolchains",
    "nixpkgs_python_toolchain",
)

register_toolchains(
    "@nixpkgs_config_cc_toolchains//:all",
    "@nixpkgs_python_toolchain//:toolchain",
    dev_dependency = True,
)

# For buildifier
bazel_dep(
    name = "rules_go",
    version = "0.39.1",
    dev_dependency = True,
    repo_name = "io_bazel_rules_go",
)

use_repo(
    non_module_dev_deps,
    "com_github_bazelbuild_buildtools",
    "nixpkgs_go_sdk_toolchains",
)

register_toolchains(
    "@nixpkgs_go_sdk_toolchains//:all",
    dev_dependency = True,
)

go_sdk = use_extension(
    "@io_bazel_rules_go//go:extensions.bzl",
    "go_sdk",
    dev_dependency = True,
)
go_sdk.download(
    version = "1.19.7",
)

bazel_dep(
    name = "gazelle",
    version = "0.30.0",
    dev_dependency = True,
)

go_deps = use_extension(
    "@gazelle//:extensions.bzl",
    "go_deps",
    dev_dependency = True,
)
go_deps.module(
    path = "github.com/gogo/protobuf",
    sum = "h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=",
    version = "v1.3.2",
)
use_repo(
    go_deps,
    "com_github_gogo_protobuf",
    "com_github_golang_mock",
    "com_github_golang_protobuf",
    "org_golang_google_genproto",
    "org_golang_google_grpc",
    "org_golang_google_protobuf",
    "org_golang_x_net",
    "org_golang_x_tools",
)

# For documentation
use_repo(
    non_module_dev_deps,
    "graphviz",
    "linux_pandoc",
    "macos_pandoc",
    "nixpkgs_pandoc",
    "sphinx",
    "zip",
)

register_toolchains(
    "@rules_haskell//docs/pandoc:nixpkgs",
    "@rules_haskell//docs/pandoc:linux",
    "@rules_haskell//docs/pandoc:macos",
    "@rules_haskell//tests:protobuf-toolchain",
    "@rules_haskell//tests:protobuf-toolchain-osx_arm64",
    dev_dependency = True,
)

stack_snapshot = use_extension(
    "@rules_haskell//extensions:stack_snapshot.bzl",
    "stack_snapshot",
)

# The @stackage workspace is referenced in the `stack_snapshot` macro to
# forward the generated `components.json` file to the `_stack_executables`
# repository rule which generates the `@stackage-exe` workspace. This reference
# is resolved within the scope of rules_haskell and therefore requires
# `@stackage` to be imported.
# See https://github.com/tweag/rules_haskell/blob/e31617dcd76264f00a833625f70e1e46e91fbc57/haskell/cabal.bzl#L2709
# TODO[AH] Find a way to not require an import of `@stackage` into rules_haskell.
use_repo(
    stack_snapshot,
    "stackage",
)

stack_snapshot_dev = use_extension(
    "@rules_haskell//extensions:stack_snapshot.bzl",
    "stack_snapshot",
    dev_dependency = True,
)

[
    stack_snapshot_dev.package(
        name = package,
    )
    for package in [
        # Core libraries
        "base",
        "bytestring",
        "containers",
        "deepseq",
        "mtl",
        "text",
        "vector",
        # For tests
        "lens-family-core",
        "data-default-class",
        "proto-lens",
        "lens-family",
    ]
]

stack_snapshot_dev.package(
    name = "ghc-paths",
    vendored = "@rules_haskell//tools/ghc-paths",
)
stack_snapshot_dev.package(
    name = "proto-lens-protoc",
    components = [
        "lib",
        "exe",
    ],
    setup_deps = ["@Cabal//:Cabal"],
)

[
    # See https://github.com/tweag/rules_haskell/issues/1871
    stack_snapshot_dev.package(
        name = package,
        setup_deps = ["@Cabal//:Cabal"],
    )
    for package in [
        "HUnit",
        "bifunctors",
        "call-stack",
        "generic-deriving",
        "mono-traversable",
        "proto-lens-runtime",
        "quickcheck-io",
        "transformers-compat",
        "type-errors",
        "typed-process",
        "unliftio-core",
    ]
]

stack_snapshot_dev.stack_snapshot_json(label = "@rules_haskell//:stackage_snapshot.json")
stack_snapshot_dev.snapshot(
    local_snapshot = "@rules_haskell//:stackage_snapshot.yaml",
)
use_repo(
    stack_snapshot_dev,
    "stackage-exe",
    "stackage-unpinned",
)
